<?php
/**
 * right now列表公共控制器
 * rightnowlistcontroller.php
 * Create on 2015-04-25 11:00:06
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\lists;

class RightNowListController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	const DEFAULT_LIST_PAGE_NUM = 5;
	public function __construct($updateTime = null,$controlId,$isPublish){
		
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	
	/**
	 * 获取模版内容
	 * @param : String $content
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$currentPage = $this->getParameter(self::$CURRENTPAGE);
		
		
		$pageable = $this->getParameter(self::$PAGEABLE);
		$siteChannelTag = $this->getParameter(self::$SITE_CHANNEL_TAG);
		
		$this->assign('siteTemplateId', $this->getParameter(self::$SITE_TEMPLATE_ID));
		if($controlData['manual']){
			
			if(isset($controlData['manual']['topTagId'])) {
				
				$this->parseTagId($controlData['manual']['topTagId'],$controlData['manual']['topTagId']);
			};

			
			$fileName = $this->getParameter(self::$FILE_NAME);
			$this->assign('viewName','publish');
			$topIds = self::getTopTags();
			if(is_array($this->topTagId)){
				$url = $topIds[$this->topTagId[0]]['channelurl'];
			}else{
				$url = $topIds[$this->topTagId]['channelurl'];
			}
			$url .= '/'.trim($fileName,'/').'/';
			$url = preg_replace('/\/channel/i','', $url);
			$this->assign('pageUrl',$url);
			
			$path = $this->generateHtmlPath(__CLASS__,$this->conditionTagId);
			$dataCondition = $controlData['manual'];
			$dataCondition['currentPage'] = $currentPage;
			
			$this->getArticleNewList($dataCondition);
			
			$this->generateHtml($path,$content);
			$content = $this->getHtml($path);
			$content = $this->replaceCDNLink($content);

			$this->initTag();
			return $content;
		}
	}
	
	/**
	 * 获取最新资讯列表
	 * @param : Array $queryCondition 查询条件
	 */
	private function getArticleNewList($dataCondition){
// 		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
// 		if($this->conditionTagId == \haibao\cms\business\control\PublishController::NO_STRUCTURE_TAG){
// 			$dataCondition['level'] = 5;
// 			$dataCondition['biglevel'] = 7;
// 		}else{
// 			$dataCondition['level'] = 4;
// 			$dataCondition['biglevel'] = 6;
// 		}
// 		$dataCondition['isProImage'] = true;
// 		$dataCondition['isImageColl'] = false;
// 		$dataCondition['isTag'] = true;
// 		$dataCondition['isReturnTag'] = true;
// 		$dataCondition['isPrimaryTag'] = true;
// 		$dataCondition['isParentTag'] = true;
		
// 		if($this->conditionTagId == \haibao\cms\business\control\PublishController::NO_STRUCTURE_TAG){
// 			$dataCondition['isConditionTag'] = false;
// 		}else{
// 			$dataCondition['isConditionTag'] = true;
// 		}
		
// 		$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
		
 		//list($articles,$proImage,$tagInfo,$articleTag,$pager) = $this->getArticleControl(5,$dataCondition);
		if($this->conditionTagId == \haibao\cms\business\control\PublishController::NO_STRUCTURE_TAG){
			$dataCondition['isConditionTag'] = false;
		}else{
			$dataCondition['isConditionTag'] = true;
		}
 		list($articles,$proImage,$tagInfo,$articleTag,$pager) = $this->getArticleList($dataCondition);

		$finalArr = array();
		foreach($articles as $key=>$article){
			$temp = array();
			$temp['Id'] = $article->Id;
			$temp['Title'] = $article->Title;
			$temp['SiteType'] = $article->SiteType;
			$temp['Channel'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
			$temp['Date'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
			
			if(isset($tagInfo[$article->ArticleTag->TagId])){

				$temp['TagName'] = $tagInfo[$article->ArticleTag->TagId]['TagName'];
				$temp['TagUrl'] = $tagInfo[$article->ArticleTag->TagId]['Url'];
			}else{
				$temp['TagUrl'] = $temp['TagName'] = '';
			}

			if(isset($proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
				$temp['ProImage'] = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
			}else{
				if(isset($proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
					$temp['ProImage'] = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}else{
					$temp['ProImage'] = '';
				}
			}
			
			array_push($finalArr,$temp);
				
		}
		$topList = $bottomList = array();
		$this->assign('pageCount',$pager->getPageCount());
		$this->assign('pager', $pager);
		$this->assign('topList',$finalArr);
	}
	
	private function getArticleList($dataCondition){
	    $articleEntity = new \haibao\cms\business\control\pc\entity\ArticleEntity();
	    $articleEntity->setLevel(4);
	    $articleEntity->setBigLevel(7);
	    $articleEntity->setLimit($dataCondition['limit']);
	    $articleEntity->setIsProImage(true);
	    $articleEntity->setIsLeftJoinTag(true);
	    $articleEntity->setIsPrimaryTag(true);
	    $articleEntity->setIsConditionTag($dataCondition['isConditionTag']);
	    $articleEntity->setIsReturnTag(true);
	    $articleEntity->setIsImageColl(false);
	    $articleEntity->setIsPage(true);
	    $articleEntity->setCurrentPage($dataCondition['currentPage']);
	    $articleEntity->setIsParentTag(true);
	    $articleEntity->setSource(__CLASS__);
	    $articleEntity->setTagIds($this->conditionTagId);
	    $articleEntity->setIsDebug(false);
	    $articleEntity->setIsOnlyReturnPrimaryTag(true);
	    $articleEntity->setProImageLevel(array(\haibao\cms\model\data\ArticleProImage::Level_FOUR));
	    
	    $this->articleAction->setArticleEnity($articleEntity);
	    return $this->articleAction->getListForNew();
	   
	}
}
